#ifndef INPUT_HANDLER_
#define INPUT_HANDLER_

#include <SDL.h>
#include <vector>
#include <iostream>
#include "game.h"
#include "log.h"
#include "vector2D.h"

enum MouseButtons {
    kLeft = 0,
    kMiddle = 1,
    kRight = 2
};

class InputHandler {
private:
    InputHandler();
    ~InputHandler();

    std::vector<SDL_Joystick*> joysticks_;  // 初始化成功即可使用，放在数组里面只是为了后面释放方便
    bool joysticks_initialized_;
    std::vector<std::pair<Vector2D*, Vector2D*> > joystick_value_;  // 2 dimensions because of perhaps there are more than one controller.
    const int kJoystickDeadZone_ = 10000;  // limit joy's sensitivity
    std::vector<std::vector<bool> > button_states_;  // joystick buttons

    std::vector<bool> mouse_button_states_;  // 1 dimension we assume here is one mouse.
    Vector2D* mouse_position_;
    const Uint8* key_state_;  // SDL has an array contains all of keys's state.

    // private functions to handle different event types (Otherwise the Update function too large)
    // handle keyboard events
    void OnKeyDown_();
    void OnKeyUp_();

    // handle mouse events
    void OnMouseMove_(SDL_Event& event);
    void OnMouseButtonDown_(SDL_Event& event);
    void OnMouseButtonUp_(SDL_Event& event);

    // handle joystick events
    void OnJoystickAxisMove_(SDL_Event& event);
    void OnJoystickButtonDown_(SDL_Event& event);
    void OnJoystickButtonUp_(SDL_Event& event);

public:
    static InputHandler& instance() {
        static InputHandler instance;
        return instance;
    }
    void Update();
    void Clean();
    void InitializeJoysticks();
    bool JoysticksInitialezed() { return joysticks_initialized_; }
    int Xvalue(int joy, int stick);
    int Yvalue(int joy, int stick);
    bool get_button_states(int joy, int button_number);
    bool get_mouse_button_states_(int button_number);
    Vector2D* get_mouse_position();
    bool IsKeyDown(SDL_Scancode key);
    void Reset();

};
typedef InputHandler TheIH;

#endif // INPUT_HANDLER_
